var urltest = sessionStorage.getItem("urltest");
var userId = sessionStorage.getItem("userId");
var fullname = sessionStorage.getItem("fullname");

$(function() {
	if(userId == "" || userId == undefined || userId == null) {
		top.location.href = "../login.html";
	} else {
		wuye_name();
	}
});
//加载物业名称
function wuye_name(data) {
	var parentid = data;
	var reque = function() {
		return {
			parentid: parentid,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/property/selectOrganizeNameList',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				//								console.log(msg);
				console.log(msg.returnString);
				var bb = JSON.parse(msg.returnString);
				//				var str = "";
				var str = '<option value="">' + "请选择物业名称" + '</option>';
				for(var i = 0; i < bb.length; i++) {
					str += "<option value=" + bb[i].organizeid + ">" + bb[i].shortname + "</option>";
				}
				$("#select_wuye").html("");
				$("#select_wuye").append(str);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
}
//物业改变查小区
$("#select_wuye").change(function() {
	var scdd_shxz = $("#select_wuye").val();
	if(scdd_shxz == '') {
		var str = '<option value="">' + "请选择小区名称" + '</option>';
		$("#select_xiaoqu").html("");
		$("#select_xiaoqu").append(str);
	} else {
		xiaoqu_name(scdd_shxz)
	}
})
//加载小区名称
function xiaoqu_name(data) {
	var parentid = data;
	var reque = function() {
		return {
			merchantid: parentid,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	if(parentid != "") {
		$.ajax({
			contentType: "application/json; charset=utf-8",
			type: "POST",
			url: urltest + 'verify/property/selectAreaName',
			data: req3,
			async: true,
			dataType: "json",
			success: function(msg) {
				//			console.log(msg);
				if(msg.responseCode == 200) {
					//				console.log(msg);
					console.log(msg.returnString);
					var bb = JSON.parse(msg.returnString);
					//				var str = "";
					var str = '<option value="">' + "请选择小区名称" + '</option>';
					for(var i = 0; i < bb.length; i++) {
						str += "<option value=" + bb[i].areaid + ">" + bb[i].areaname + "</option>";
					}
					$("#select_xiaoqu").html("");
					$("#select_xiaoqu").append(str);
				} else {
					alert(msg.resoponseDisp)
				}
			},
			error: function() {
				alert("获取信息失败");
			}
		});
	}

}
//加载数据
function ajax_data(data) {
	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/financial/selectStatisticsListForOrderKind',
		data: data,
		async: false,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				var bb = JSON.parse(msg.returnString);
				$("#count").html(msg.count);
				//	pay_app_count  pay_app pay_wuye_count pay_wuye

				$("#pay_app_count").html(bb.count1+'条');
				$("#pay_app").html(bb.paymentamt1+'元');
				$("#pay_wuye_count").html(bb.count2+'条');
				$("#pay_wuye").html(bb.paymentamt2+'元');

				$("#bi_zong").html(bb.count+'条');

				$("#bi_money").html(bb.paymentamt+'元');

				var gezi = msg.count;
				if(gezi % 10 == 0) {
					var lenn = parseInt(gezi / 10);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 10) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: 1,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});
}

$("#bp-3-element-test").on("click", function() {
	var aa = $(this).children(".active").children("a").html();
	var pageNumber = aa;
	var pageSize = "10";

	var merchantid = $("#select_wuye").val();
	var areaid = $("#select_xiaoqu").val();

	var star_time = $('.input_1').val();
	var end_time = $('.input_2').val();
	var reque = function() {
		return {
			pageSize: pageSize,
			pageNumber: pageNumber,
			startday: star_time,
			endday: end_time,
			merchantid: merchantid,
			areaid: areaid
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);

	$.ajax({
		contentType: "application/json; charset=utf-8",
		type: "POST",
		url: urltest + 'verify/financial/selectStatisticsListForOrderKind',
		data: req3,
		async: true,
		dataType: "json",
		success: function(msg) {
			console.log(msg);
			if(msg.responseCode == 200) {
				console.log(msg);
				$("#count").html(msg.count);
				var gezi = msg.count;
				if(gezi % 10 == 0) {
					var lenn = parseInt(gezi / 10);
					if(lenn == 0) {
						var lenn = 1;
					}
				} else {
					var lenn = parseInt(gezi / 10) + 1;
				}
				test("Test bootstrap v3 rendering", function() {
					var element = $('#bp-3-element-test');
					var options = {
						bootstrapMajorVersion: 3,
						currentPage: aa,
						numberOfPages: 5,
						totalPages: lenn
					}
					element.bootstrapPaginator(options);
					var element = $('#bp-3-element-test');
					ok(!element.hasClass('pagination-lg'), "Root element shouldn't have pagination-lg class");
					ok(!element.hasClass('pagination-sm'), "Root element shouldn't have pagination-sm class");
					var list = element.children();
					for(var i = 0; i < list.length; i++) {
						var item = $(list[i]);
						ok(item.is("li"), "Element " + i + " should be li");
					}
				});
				var table_msg = msg.returnString;
				createShowingTable(table_msg);
			} else {
				alert(msg.resoponseDisp)
			}
		},
		error: function() {
			alert("获取信息失败");
		}
	});

})

function createShowingTable(bb) {
	//获取后台传过来的jsonData,并进行解析  
	//此处需要让其动态的生成一个table并填充数据 
	if(bb.total == 0) {
		$("#tbbb").html("");
	} else {
		var bb = JSON.parse(bb);
		console.log(bb);
		var tableStr = "";
		var len = bb.list.length;

		if(len == 0) {
			var wuxiaoxi = '<p>' + "暂无内容" + '</p>'
			$("#tbbb").html(wuxiaoxi);
		} else {
			for(var i = 0; i < len; i++) {
				//				0 未支付 1 已支付 2 已取消
				//				3 已退款 4 缴费失败， 已退款
				//				if(bb[i].username == undefined) {
				//					bb[i].username = '';
				//				}
				//
				//				if(bb[i].apipayid == undefined) {
				//					bb[i].apipayid = ""
				//				}
				if(bb.list[i].chargeway == 0) {
					//					 0app 1物业端       
					bb.list[i].chargeway = "app"
				}else if(bb.list[i].chargeway == 1){
					bb.list[i].chargeway = "物业端"
				}
				var num = parseInt(i) + parseInt(1);
				tableStr = tableStr + '<tr>' +
					'<th>' + num + '</th>' +
					'<th>' + bb.list[i].merchantname + '</th>' +
					'<th>' + bb.list[i].areaname + '</th>' +
					'<th>' + bb.list[i].billkindname + '</th>' +
					
					
					'<th>' + bb.list[i].xianjin + '</th>' +
					'<th>' + bb.list[i].pos + '</th>' +
					'<th>' + bb.list[i].weixin + '</th>' +
					'<th>' + bb.list[i].ali + '</th>' +
					
					'<th>' + bb.list[i].count + '</th>' +
					'<th style="color: red;">' + bb.list[i].paymentamt + '</th>' +
					'</tr>';
			}
			//将动态生成的table添加的事先隐藏的div中.  
			$("#tbbb").html(tableStr);

		}

	}
}

//模糊查询
$('#search_chaxun').click(function() {
	var merchantid = $("#select_wuye").val();
	var areaid = $("#select_xiaoqu").val();
	var pageNumber = "1";
	var pageSize = "10";
	var star_time = $('.input_1').val();
	var end_time = $('.input_2').val();
	var reque = function() {
		return {
			pageSize: pageSize,
			pageNumber: pageNumber,
			startday: star_time,
			endday: end_time,
			merchantid: merchantid,
			areaid: areaid,
		}
	}
	rq1 = reque();
	var req = function() {
		return {
			requestString: "",
			userId: userId,
			fullname: fullname
		}
	}
	requestObj1 = req();
	requestObj1.requestString = JSON.stringify(rq1);
	var req3 = JSON.stringify(requestObj1);
	ajax_data(req3)

})